package znbkListCtfd.fargment;

import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.lancooznbk.R;
import znbkListCtfd.entity.WrongTopicTutoringListEntity;
import znbkListCtfd.presenter.WrongTopicTutoringListPresenter;
import com.example.lancooznbk.view.adapter.PopupMenuType1Adapter;
import znbkListCtfd.adapter.WrongTopicTutoringListAdapter;
import com.example.lancooznbk.view.base.BaseMainFragment;
import com.example.lancooznbk.view.base.ILoadMoreDataView;
import com.example.lancooznbk.view.widget.CustomFooterView;
import com.example.lancooznbk.view.widget.PopupMenu;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018\2\1 0001.
 */

public class WrongTopicTutoringListFragment extends BaseMainFragment<WrongTopicTutoringListPresenter> implements View.OnClickListener,ILoadMoreDataView<WrongTopicTutoringListEntity> {

    private TextView mTvQuesTotal;
    private TextView mTvSpQuesType;
    private TextView mTvSPQuesStatus;
    private View mViewMasker;
    private RelativeLayout mRelToolHead;
//    private Toolbar mToolbar;

    private PopupMenu mQuesTypePopupMenu;
    private PopupMenu mQuesStatusPopupMenu;
    private PopupMenuType1Adapter mQuesTypeAdapter;
    private PopupMenuType1Adapter mQuesStatusAdapter;
    private WrongTopicTutoringListAdapter mWrongTopicTutoringListAdapter;


    private Map<String,String> mQuesTypeMap = new HashMap<>();
    private Map<String,Integer> mQuesStatusMap = new HashMap<>();

    private int checkPosition = -1;

    @Override
    public void initBaseMainViews(View view, Bundle savedInstanceState) {
        checkPosition = 0;

        mPresenter = new WrongTopicTutoringListPresenter(this);
        mViewMasker = view.findViewById(R.id.view_masker);
        mWrongTopicTutoringListAdapter = new WrongTopicTutoringListAdapter(getHolderActivity());
        mWrongTopicTutoringListAdapter.setCustomLoadMoreView(new CustomFooterView(getActivity()));
        mRecyclerView.setAdapter(mWrongTopicTutoringListAdapter);
        mTvSpQuesType = (TextView) view.findViewById(R.id.sp_quesType);
        mTvSPQuesStatus = (TextView) view.findViewById(R.id.sp_status);
        mTvQuesTotal = (TextView) view.findViewById(R.id.tv_quesTotal);
        mRelToolHead = (RelativeLayout) view.findViewById(R.id.rel_toolbar_head);
        View toolBarLayout = view.findViewById(R.id.rel_toolbar_layout);
        ((TextView)toolBarLayout.findViewById(R.id.tv_toolbar_title)).setText(R.string.menu_ctfd);
        ((LinearLayout)toolBarLayout.findViewById(R.id.layout_toolbar_back)).setOnClickListener(this);

        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) toolBarLayout.getLayoutParams();
        layoutParams.setMargins(0,getStatusBarHeight(),0,0);

        mTvSpQuesType.setOnClickListener(this);
        mTvSPQuesStatus.setOnClickListener(this);
        mQuesTypeAdapter = new PopupMenuType1Adapter(getHolderActivity());
        mQuesStatusAdapter = new PopupMenuType1Adapter(getHolderActivity());
        mQuesTypePopupMenu = new PopupMenu(getHolderActivity(), ViewGroup.LayoutParams.MATCH_PARENT,R.drawable.bg_popupmenu);
        mQuesStatusPopupMenu = new PopupMenu(getHolderActivity(), ViewGroup.LayoutParams.MATCH_PARENT,R.drawable.bg_popupmenu);

        String[] quesStatusArrays = getResources().getStringArray(R.array.QuesStatusArraysCtfd);
        String[] quesStatusName = new String[quesStatusArrays.length];
        for (int i = 0 ; i < quesStatusArrays.length ; i++){
            String[] split = quesStatusArrays[i].split("\\|");
            quesStatusName[i] = split[0];
            mQuesStatusMap.put(split[0], Integer.valueOf(split[1]));
        }


        mQuesStatusAdapter = new PopupMenuType1Adapter(getHolderActivity(),quesStatusName);
        mQuesStatusPopupMenu.setListAdapter(mQuesStatusAdapter);
        mQuesStatusPopupMenu.setCheckIndex(0);
        mTvSPQuesStatus.setText(mQuesStatusPopupMenu.getCheckedStr());
        mQuesStatusPopupMenu.setOnItemClickListener(new PopupMenu.OnItemClickListener() {
            @Override
            public void onItemClick(View item, int position, long id) {
                mTvSPQuesStatus.setText(mQuesStatusPopupMenu.getCheckedStr());

                checkPosition = 0;
                mTvSpQuesType.setText("全部题型");

                mXRefreshView.startRefresh();
                mPresenter.getDataByStatus(mQuesStatusMap.get(mQuesStatusPopupMenu.getCheckedStr()));
            }
        });

        mQuesStatusPopupMenu.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                mTvSPQuesStatus.setSelected(false);
                mViewMasker.setVisibility(View.GONE);
            }
        });

        mWrongTopicTutoringListAdapter.setOnItemClickListener(new WrongTopicTutoringListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, List<WrongTopicTutoringListEntity.ParrentQuesListBean> list, int position) {
                mPresenter.openItemDetail(position,list.get(position));
            }
        });

        mWrongTopicTutoringListAdapter.setOnItemHistoryClickListener(new WrongTopicTutoringListAdapter.OnItemHistoryClickListener() {
            @Override
            public void onItemClick(View view, List<WrongTopicTutoringListEntity.ParrentQuesListBean> list, int position) {
                mPresenter.openItemHistoryDetail(position,list.get(position));
            }
        });
    }

    @Override
    protected void updateViews(boolean isShowLoading) {
        mPresenter.getData(isShowLoading);
    }

    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_list_wrong_topic_tutoring;
    }

    @Override
    public void loadData(WrongTopicTutoringListEntity data) {
        if(mRecyclerView.getLayoutManager() != null ) {
            mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
        }


        String[] quesTypeNames = new String[data.getExistQuesType().size()+1];
        String[] quesTypeTypeIDs = new String[data.getExistQuesType().size()+1];

        for (int i = 0 ; i < data.getExistQuesType().size()+1 ; i++){
            if(i==0){
                quesTypeNames[i] = "全部题型";
                quesTypeTypeIDs[i] = "";
            }else {
                quesTypeNames[i] = data.getExistQuesType().get(i-1).getTypeName();
                quesTypeTypeIDs[i] = data.getExistQuesType().get(i-1).getTypeID();
            }
            mQuesTypeMap.put(quesTypeNames[i],quesTypeTypeIDs[i]);
        }

        mQuesTypeAdapter = new PopupMenuType1Adapter(getHolderActivity(),quesTypeNames,checkPosition);
        mQuesTypePopupMenu.setListAdapter(mQuesTypeAdapter);
        mTvSpQuesType.setText(mQuesTypePopupMenu.getCheckedStr());
        mQuesTypePopupMenu.setOnItemClickListener(new PopupMenu.OnItemClickListener() {
            @Override
            public void onItemClick(View item, int position, long id) {
                checkPosition = position;
                mTvSpQuesType.setText(mQuesTypePopupMenu.getCheckedStr());
                mXRefreshView.startRefresh();
                mPresenter.getDataByQuesType(mQuesTypeMap.get(mQuesTypePopupMenu.getCheckedStr()));
            }
        });

        mQuesTypePopupMenu.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                mTvSpQuesType.setSelected(false);
                mViewMasker.setVisibility(View.GONE);
            }
        });



        setQuesTotal(data.getTotalQues());
        mWrongTopicTutoringListAdapter.clearAndAdd(data.getParrentQuesList());
        loadComplete(mWrongTopicTutoringListAdapter.getAdapterItemCount() >= data.getTotalQues());

    }


    @Override
    public void loadMoreData(WrongTopicTutoringListEntity data) {
        setQuesTotal(data.getTotalQues());
        mWrongTopicTutoringListAdapter.add(data.getParrentQuesList());

        loadComplete(mWrongTopicTutoringListAdapter.getAdapterItemCount() >= data.getTotalQues());
    }

    @Override
    public void showNetError() {
        super.showNetError();
        setQuesTotal(0);

        String[] quesTypeNames = new String[1];
        String[] quesTypeTypeIDs = new String[1];
        quesTypeNames[0] = "全部题型";
        quesTypeTypeIDs[0] = "";
        mQuesTypeMap.put(quesTypeNames[0],quesTypeTypeIDs[0]);

        mQuesTypeAdapter = new PopupMenuType1Adapter(getHolderActivity(),quesTypeNames,checkPosition);
        mQuesTypePopupMenu.setListAdapter(mQuesTypeAdapter);
        mTvSpQuesType.setText(mQuesTypePopupMenu.getCheckedStr());
        mQuesTypePopupMenu.setOnItemClickListener(new PopupMenu.OnItemClickListener() {
            @Override
            public void onItemClick(View item, int position, long id) {
                checkPosition = position;
                mTvSpQuesType.setText(mQuesTypePopupMenu.getCheckedStr());
                mXRefreshView.startRefresh();
                mPresenter.getDataByQuesType(mQuesTypeMap.get(mQuesTypePopupMenu.getCheckedStr()));
            }
        });

        mQuesTypePopupMenu.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                mTvSpQuesType.setSelected(false);
                mViewMasker.setVisibility(View.GONE);
            }
        });

    }

    @Override
    public void showNoData(String msg) {
        super.showNoData(msg);
        setQuesTotal(0);

        String[] quesTypeNames = new String[1];
        String[] quesTypeTypeIDs = new String[1];
        quesTypeNames[0] = "全部题型";
        quesTypeTypeIDs[0] = "";
        mQuesTypeMap.put(quesTypeNames[0],quesTypeTypeIDs[0]);

        mQuesTypeAdapter = new PopupMenuType1Adapter(getHolderActivity(),quesTypeNames,checkPosition);
        mQuesTypePopupMenu.setListAdapter(mQuesTypeAdapter);
        mTvSpQuesType.setText(mQuesTypePopupMenu.getCheckedStr());
        mQuesTypePopupMenu.setOnItemClickListener(new PopupMenu.OnItemClickListener() {
            @Override
            public void onItemClick(View item, int position, long id) {
                checkPosition = position;
                mTvSpQuesType.setText(mQuesTypePopupMenu.getCheckedStr());
                mXRefreshView.startRefresh();
                mPresenter.getDataByQuesType(mQuesTypeMap.get(mQuesTypePopupMenu.getCheckedStr()));
            }
        });

        mQuesTypePopupMenu.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                mTvSpQuesType.setSelected(false);
                mViewMasker.setVisibility(View.GONE);
            }
        });

    }

    /**
     * 更新题目总数
     * @param totalCount
     */
    private void setQuesTotal(int totalCount) {
        mTvQuesTotal.setText(Html.fromHtml(String.format(getString(R.string.znbkQuesTotal),totalCount)));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.sp_quesType:
                mViewMasker.setVisibility(View.VISIBLE);
                mQuesTypePopupMenu.showLocation(mRelToolHead,0,0);
                mTvSpQuesType.setSelected(true);
                break;
            case R.id.sp_status:
                mViewMasker.setVisibility(View.VISIBLE);
                mQuesStatusPopupMenu.showLocation(mRelToolHead,0,0);
                mTvSPQuesStatus.setSelected(true);
                break;
            case R.id.layout_toolbar_back:
                getHolderActivity().finish();
                break;
        }
    }

}